package data;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import logic.hashfunctions.AbstractHashFuncion;

public class Dictionary {
	
	String name;
	List<String> words = new ArrayList<String>();
	List<Integer> hashes = new ArrayList<Integer>();
	
	public Dictionary(String name, String filepath, AbstractHashFuncion hashFunction) {

		words = getWordsFromFile(filepath);
		populateDictionary(words, hashFunction);
		this.name = name;
	}
	
	public Dictionary(String name, List<String> words, AbstractHashFuncion hashFunction) {
		
		this.words = words;
		populateDictionary(words, hashFunction);
		this.name = name;
	}
	
	public String getName(){
		
		return name;
	}

	public static List<String> getWordsFromFile(String filepath){
		
		List<String> words = new ArrayList<String>();

		try {

			BufferedReader br = new BufferedReader(new FileReader(filepath));
			String word = br.readLine();

			do {
				words.add(word);
				word = br.readLine();

			} while (word != null);

			br.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return words;
	}
	
	private void populateDictionary(List<String> words, AbstractHashFuncion hashFunction) {

		hashes = hashFunction.generateHashes(words);
		
	}	
	
	public List<String> getWords(){
		return words;
	}
	
	public List<Integer> getHashes(){
		return hashes;
	}
	
}
